package com.game.worm.mobile.view;

import android.os.Bundle;
import android.view.View;

import com.dx.as.ToastUtil;
import com.dx.as.ToolBarOptions;
import com.dx.as.UI;
import com.dx.as.common.FragmentUtils;
import com.game.worm.mobile.R;
import com.game.worm.mobile.view.discovery.DiscoveryFragment;
import com.game.worm.mobile.view.download.DownloadActivity;
import com.game.worm.mobile.view.find.FindGameFragment;
import com.game.worm.mobile.view.home.HomeFragment;
import com.game.worm.mobile.view.list.ListFragment;
import com.game.worm.mobile.view.message.MessageActivity;
import com.game.worm.mobile.view.mine.MineFragment;
import com.game.worm.mobile.view.search.SearchActivity;

public class MainActivity extends UI {

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        initToolBarView();
        putDefaultFragment();
    }

    private void putDefaultFragment(){
        FragmentUtils.putDefaultFragment(this,R.id.container,new HomeFragment());
    }

    private void initToolBarView(){
        ToolBarOptions toolBarOptions = new ToolBarOptions();
        toolBarOptions.toolView = View.inflate(this,R.layout.home_search_game,null);
        toolBarOptions.toolView.findViewById(R.id.search).setOnClickListener(onClickListener);
        toolBarOptions.toolView.findViewById(R.id.message).setOnClickListener(onClickListener);
        toolBarOptions.toolView.findViewById(R.id.download).setOnClickListener(onClickListener);
        setToolbar(toolBarOptions);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.search: //点击搜索
                    goToNewActivity(SearchActivity.class);
                    break;
                case R.id.message: //点击信息
                    goToNewActivity(MessageActivity.class);
                    break;
                case R.id.download:
                    goToNewActivity(DownloadActivity.class);
                    break;
            }
        }
    };

    public void onMenuChange(View view){
        switch (view.getId()){
            case R.id.rb1://首页
                FragmentUtils.goToFragment(this,HomeFragment.class,R.id.container);
                break;
            case R.id.rb2://榜单
                FragmentUtils.goToFragment(this,ListFragment.class,R.id.container);
                break;
            case R.id.rb3://发现
                FragmentUtils.goToFragment(this,DiscoveryFragment.class,R.id.container);
                break;
            case R.id.rb4://我的
                FragmentUtils.goToFragment(this,MineFragment.class,R.id.container);
                break;
            case R.id.rb5://找游戏
                FragmentUtils.goToFragment(this,FindGameFragment.class,R.id.container);
                break;
        }
    }

    private long previousTime;
    @Override
    public void onBackPressed() {
        long current = System.currentTimeMillis();
        if (previousTime <= 0 || current - previousTime >= 1000){
            ToastUtil.showToast(this,"在按一次退出！");
            previousTime = current;
            return;
        }
        System.exit(0);
    }


}
